package com.gmu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gmu.domain.LoginUserDetails;
import com.gmu.mapper.TblMenuMapper;
import com.gmu.mapper.TblUserMapper;
import com.gmu.pojo.TblUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * @author 木鱼~
 * @version 1.0
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private TblUserMapper tblUserMapper;

    @Autowired
    private TblMenuMapper tblMenuMapper;

    @Override
    public UserDetails loadUserByUsername(String idNumber) throws UsernameNotFoundException {
        LambdaQueryWrapper<TblUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TblUser::getIdNumber,idNumber);
        TblUser tblUser = tblUserMapper.selectOne(wrapper);
        //如果查不到数据就抛出异常
        if (Objects.isNull(tblUser)){
            return null;
        }
        //TODO 根据用户查询权限信息 添加到LoginUser中
        List<String> perms = tblMenuMapper.getPermsByUserId(tblUser.getUserId());
        return new LoginUserDetails(tblUser,perms);
    }
}
